<template>
	<view class="register-container">
		<view class="register-logo-container">
			<image class="register-logo" src="/static/logo.png" mode="scaleToFill"></image>
		</view>

		<view class="register-form-container">
			<form class="register-form" @submit="register">
				<input class="register-input" type="text" placeholder="请输入账号" name="account"
					placeholder-style="color:#cecece;font-weight: bold;" />
				<input class="register-input" type="text" placeholder="请输入密码" name="password"
					placeholder-style="color:#cecece;font-weight: bold;" password />
				<input class="register-input" type="text" placeholder="确认密码" name="repassword"
					placeholder-style="color:#cecece;font-weight: bold;" password />
				<input class="register-input" type="text" placeholder="请输入昵称" name="name"
					placeholder-style="color:#cecece;font-weight: bold;" />
				<input class="register-input" type="tel" placeholder="请输入手机号" name="tel"
					placeholder-style="color:#cecece;font-weight: bold;" />
				<input class="register-input" type="email" placeholder="请输入邮箱" name="email"
					placeholder-style="color:#cecece;font-weight: bold;" />
				<view class="register-btn-container">
					<button class="register-btn" form-type="submit">注册</button>
				</view>
				<view class="tips">
					<text>已有账号？</text>
					<navigator url="/pages/login/login" open-type="redirect"><text class="go-register-text">去登录</text>
					</navigator>
				</view>
			</form>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {

			}
		},
		methods: {
			register(e) {
				uni.showLoading({
					title: "注册中"
				})

				let {
					account,
					password,
					repassword,
					name,
					tel,
					email
				} = e.detail.value;


				if (!account) {
					uni.showToast({
						title: '用户名不能为空',
						icon: 'error',
					})
					return;
				}

				if (!password) {
					uni.showToast({
						title: '密码不能为空',
						icon: 'error',
					})
					return;
				}

				if (password != repassword) {
					uni.showToast({
						title: '两次密码不一致',
						icon: 'error',
					})
					return;
				}

				if (!name) {
					uni.showToast({
						title: '昵称不能为空',
						icon: 'error',
					})
					return;
				}

				if (!tel) {
					uni.showToast({
						title: '手机号不能为空',
						icon: 'error',
					})
					return;
				}

				if (!email) {
					uni.showToast({
						title: '邮箱不能为空',
						icon: 'error',
					})
					return;
				}

				uniCloud.callFunction({
					name: 'addUser',
					data: {
						account,
						password
					},
					success: (res) => {

						if (res.result.code == 200) {
							uniCloud.callFunction({
								name: 'addUserInfo',
								data: {
									user_id: res.result.accountId.id,
									name,
									tel,
									email
								},
								success: (res) => {
									if (res.result.code == 200) {
										uni.showToast({
											title: res.result.msg,
											icon: 'success',
											mask: true
										})
										
										
										setTimeout(() => {
										
											uni.navigateBack();
										}, 1000)
									}
								}
							})

						} else {
							uni.showToast({
								title: res.result.msg,
								icon: 'error',
								mask: true
							})
						}

					},
					fail: (err) => {
						console.log("addUser callback" + err);
					}
				})

			}
		}
	}
</script>

<style>
	.register-container {
		height: 80%;
		display: flex;
		flex-direction: column;
		justify-content: center;
	}

	.register-logo-container {
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.register-logo {
		border-radius: 50%;
		width: 200rpx;
		height: 200rpx;
	}

	.register-form-container {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	.register-form {
		width: 70%;
	}

	.register-input {
		width: 100%;
		margin-top: 30rpx;
		height: 80rpx;
		border-radius: 20rpx;
		background-color: white;
		text-align: center;
		font-size: 28rpx;
	}

	.register-btn-container {
		margin-top: 50rpx;
	}

	.register-btn {
		background-color: #0099ff;
		color: white;
		border-radius: 20rpx;
		font-size: 30rpx;
	}

	.tips {
		display: flex;
		justify-content: center;
		margin-top: 30rpx;
		font-size: 28rpx;
	}

	.go-register-text {
		color: #0099ff;
	}
</style>